<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>help center chat</title>

		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
		<script type="text/javascript" src="js/ajax-pushlet-client.js">
</script>
		<script type="text/javascript" src="js/util.js">
</script>
		<script type="text/javascript">
<!--
      var chatDoc;
      var chatFrame;
      var nick=getPageParameter('nick', 'anon');
      var customerNick = getPageParameter('customerNick','anon');
      //var isService=getPageParameter('isService','no');      
      var subject='';	
	  
      window.onload = chat_init;
      
      function getSubject(nick) {	
    	  alert(customerNick);
		  subject = '/chat'+customerNick;			
	  };
	 
      function chat_init(){
        if (window.frames && window.frames["chatContents"]) //IE 5 (Win/Mac), Konqueror, Safari
          chatFrame = window.frames["chatContents"];
        else if (document.getElementById("chatContents").contentWindow) //IE 5.5+, Mozilla 0.9+, Opera
          chatFrame = document.getElementById("chatContents").contentWindow;
        else //Moz < 0.9 (Netscape 6.0)
          chatFrame = document.getElementById("chatContents");

        if(chatFrame.document) //Moz 0.9+, Konq, Safari, IE, Opera
          chatDoc = chatFrame.document;
        else //Moz < 0.9 (Netscape 6.0)
          chatDoc = chatFrame.contentDocument;
        
		getSubject(nick);
       // alert(isService);
        enterChat();
        document.forms[0].msg.focus();  
        
       }

 		// Event Callback for join
		function onJoinAck(event) {
	 		appendMessage('Listening to chat');
		}

       // Event Callback: display all events
       function onData(event) {
	  		p_debug(false, "pushlet-app", 'event received event=' + event.getEvent() );
			var action = event.get('action');
			var content = '';
         if (action == 'send') {
				content = '<b>' + event.get('nick') + '</b>: <i>' + event.get('msg') + '</i>';
		} else if (action == 'enter') {
				content = '<b><i>*** ' + event.get('nick') + ' joined chat  ***</i></b>';
		} else if (action == 'exit') {
				content = '<b><i>*** ' + event.get('nick') + ' left chat  ***</i></b>';
        }
        appendMessage(content);

	}

    // Event Callback: display all events
	function onNack(event) {
	   alert('negative response from server: ' + event.getEvent() + ' reason: ' + event.get('p_reason'));
    }

      function appendMessage  (content){
        var newDiv = chatDoc.createElement("DIV");
        newDiv.innerHTML = content;
        chatDoc.getElementById("contents").appendChild(newDiv);
        chatFrame.scrollTo(0, chatDoc.getElementById("contents").offsetHeight);
      }

      function enterChat(){    	
    	p_join_listen(subject);
    	p_publish(subject, 'action', 'enter', 'nick', nick);
    	
    	p_listen('serviceOnlineList','pull');
   		p_publish('serviceOnlineList','action','serviceOnline','nick',nick);   		  		    	
       }

      function sendMsg(){
        p_publish(subject, 'action', 'send', 'nick', nick, 'msg', document.getElementById("msg").value);
        resetForm();
       }

      function leaveChat(){
        // Send exit to a chatter
        p_publish(subject, 'action', 'exit', 'nick', nick);

    	// Stop pushlet session
    	p_leave();

    	// Give some time to send the leave request to server
    	setTimeout('gotoEnter()', 500);    	
     }
      
      function loginOff(){
    	// Send exit to chatters
        p_publish(subject, 'action', 'exit', 'nick', nick);
    	  
    	// Send leave to helpCenter
		p_publish('serviceOnlineList','action','serviceOff','nick',nick)
		
    	// Stop pushlet session
    	p_leave();

    	// Give some time to send the leave request to server
    	setTimeout('gotoEnter()', 500); 
      }

     function gotoEnter() {
        window.location.href='index.jsp';
     }

     function resetForm(){
        document.forms[0].msg.value = "";
        document.forms[0].msg.focus();
     }//-->
    </script>

		<link rel="stylesheet" href="css/chat.css" type="text/css"></link>
	</head>

	<body>
		<center>
			<iframe id="chatContents" name="chatContents" src="contents.html"></iframe>
			<form action="#" onSubmit="sendMsg(); return false;">
				<input type="text" name="msg" id="msg" />
				&nbsp;
				<a href="#" onclick="javascript:leaveChat()">[end chat]</a>
				<a href="#" onclick="javascript:loginOff()">[login off]</a>
			</form>
		</center>
	</body>
</html>
